// pages/ping/ping.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        course_id: '',
        star: 4,
        images: [],
        image_show: true,
        video: [],
        video_show: true,

        desc:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            course_id: options.course_id
        })
        this.getCourseDetail();
    },

    //获取课程详情
    getCourseDetail: function() {
        let _this = this,
        course_id = _this.data.course_id;
        app._get('/api/course/getCourseDetail', {
            course_id: course_id
        }, function(response) {
            let setdata = {};
            setdata['course'] = response.data.data;
            _this.setData(setdata);

        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    //上传
    addState: function(e) {
        let _this = this;
        app._post('/api/login/teacher/addState', {
            course_id: _this.data.course_id,
            images: _this.data.images,
            video: _this.data.video,
            desc: _this.data.desc,
        }, function(response) {
            wx.navigateBack({
                delta: 0,
            })
        })
    },

    // 传图
    addImage: function() {
        var _this = this;
        let count = 9 - _this.data.images.length;
        console.log(_this.data.images.length);
        wx.chooseMedia({
            count: count,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            success(res) {
                wx.showToast({ 
                    title: '正在上传...', 
                    icon: 'loading', 
                    mask: true, 
                    duration: 3000
                   }) 
                for (let i=0;i<res.tempFiles.length;i++){
                    const tempFilePaths = res.tempFiles[i].tempFilePath;
                    wx.uploadFile({
                        header: {
                            token: wx.getStorageSync('token'),
                            "Content-Type": "json"
                        },
                        url: app.domain + '/api/file/ossupload',
                        filePath: tempFilePaths,
                        name: 'file',
                        success(response) {
                            let data = JSON.parse(response.data);
                            if (data.code !== 1) {
                                wx.showToast({
                                    title: data.msg,
                                    icon: 'none'
                                })
                            }
                            const images = _this.data.images.concat(data.data.site_url)
                            if (images.length == 9) {
                                _this.setData({
                                    image_show: false
                                })
                            }
                            _this.setData({
                                images: images
                            })

                        },
                        fail() {
                            wx.showToast({
                                title: '已取消！',
                                icon: 'none'
                            })
                        }
                    })
                }
            }
        })
    },

    //删图
    delImage: function(e) {
        let index = e.currentTarget.dataset.index,
            images = this.data.images;
        images.splice(index, 1);
        this.setData({
            images: images,
            image_show: true
        })
    },

     // 传视频
     addVideo: function() {
        var _this = this;
        let count = 9 - _this.data.video.length;
        console.log(_this.data.video.length);
        wx.chooseMedia({
            count: count,
            mediaType: ['video'],
            sourceType: ['album', 'camera'],
            success(res) {
                wx.showToast({ 
                    title: '正在上传...', 
                    icon: 'loading', 
                    mask: true, 
                    duration: 10000
                   }) 
                for (let i=0;i<res.tempFiles.length;i++){
                    const tempFilePaths = res.tempFiles[i].tempFilePath;
                    wx.uploadFile({
                        header: {
                            token: wx.getStorageSync('token'),
                            "Content-Type": "json"
                        },
                        url: app.domain + '/api/file/ossupload',
                        filePath: tempFilePaths,
                        name: 'file',
                        success(response) {
                            let data = JSON.parse(response.data);
                            if (data.code !== 1) {
                                wx.showToast({
                                    title: data.msg,
                                    icon: 'none'
                                })
                            }
                            const video = _this.data.video.concat(data.data.site_url)
                            if (video.length == 9) {
                                _this.setData({
                                    video_show: false
                                })
                            }
                            _this.setData({
                                video: video
                            })

                        },
                        fail() {
                            wx.showToast({
                                title: '已取消！',
                                icon: 'none'
                            })
                        }
                    })
                }
            }
        })
    },

    //删视频
    delVideo: function(e) {
        let index = e.currentTarget.dataset.index,
        video = this.data.video;
        video.splice(index, 1);
        this.setData({
            video: video,
            video_show: true
        })
    },

    onDescInput: function(e) {
      console.log(e.detail.value)
      this.setData({
        desc: e.detail.value
      })
    },
})